package com.smt.modules.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.common.enums.Code;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.modules.app.annotation.Login;
import com.smt.modules.app.annotation.LoginUser;
import com.smt.modules.bus.entity.*;
import com.smt.modules.bus.service.BillService;
import com.smt.modules.bus.service.ProjectService;
import com.smt.modules.bus.service.ProjectTaskService;
import org.apache.http.HttpStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import com.smt.common.base.BaseController;
import com.smt.modules.bus.vo.SubtaskVo;
import com.smt.modules.bus.service.SubtaskService;
import com.smt.common.utils.PageUtils;
import com.smt.common.utils.R;



/**
 * 子任务表
 *
 * @author murong
 * @email 981620741@qq.com
 * @date 2018-09-29 15:37:09
 */
@RestController
@RequestMapping("/app/subtask")
@Api(tags = {"AppSubtaskController"},description = "子任务表(客户端接口)")
public class AppSubtaskController extends BaseController{
    @Autowired
    private SubtaskService subtaskService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectTaskService projectTaskService;

    /**
    * 查询子任务表分页方法
    * @param params
    * @return
    */
    @PostMapping("/list")
    @ApiOperation(value = "/list",notes = "根据条件查询",response = SubtaskVo.class)
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = subtaskService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "/info/{id}",notes = "根据表ID查询",response = SubtaskVo.class)
    public R info(@PathVariable("id") Long id){
			SubtaskEntity subtask = subtaskService.selectById(id);

        return R.ok().put("subtask", subtask);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    @ApiOperation(value = "增加子任务",httpMethod = "POST")
    public R save(@RequestBody SubtaskVo subtask,@RequestAttribute("userId") Long userId){
        Long projectTaskId = subtask.getProjectTaskId();
        //检查权限
        ProjectTaskEntity projectTaskEntity =
                projectTaskService.selectById(projectTaskId);
        ProjectEntity projectEntity = null;
        if(!Objects.isNull(projectTaskEntity)) {
            projectEntity = projectService.selectById(projectTaskEntity.getProjectId());
        }
        if(!Objects.isNull(projectEntity)){
            Long createdId = projectEntity.getCreatedId();
            Long stageMemberId = projectTaskEntity.getStageMemberId();
            Long memberId = projectTaskEntity.getMemberId();
            if(userId.equals(createdId) || userId.equals(stageMemberId) || userId.equals(memberId)){
                return subtaskService.createSubTask(subtask,userId);
            }else{
                return R.error(EnumToKeyUtil.toInt(Code.MISSING_PERMISSION),"您没有权限创建此子任务");
            }
        }else{
            return R.error(EnumToKeyUtil.toInt(Code.APPEND_DATA_ERROR),"任务或者项目错误！");
        }

    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改子任务",response = SubtaskVo.class)
    public R update(@RequestBody SubtaskEntity subtask){
			subtaskService.updateById(subtask.copyPropertiesTo(new SubtaskEntity()));

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "/delete",notes = "批量删除",response = SubtaskVo.class)
    public R delete(@RequestBody Long[] ids){
			subtaskService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 获取该任务下的子任务集合
     */
    @GetMapping("/getSubTask/{taskId}")
    @ApiOperation(value = "获取该任务下的子任务集合",notes = "获取该任务下的子任务集合",httpMethod = "GET")
    public R getSubTask(@PathVariable(value = "taskId") Long taskId){
        return subtaskService.getSubTaskList(taskId);
    }

    /**
     * 修改子任务信息
     */
    @Login
    @PutMapping("/editTask")
    @ApiOperation(value = "修改子任务信息",httpMethod = "PUT")
    public R editTask(@RequestBody SubtaskVo subtaskVo,@RequestAttribute("userId") Long userId){
        return subtaskService.editTask(subtaskVo,userId);
    }


}
